import { XAgent } from './index';
import { marked } from 'marked';
import './style.css';
function doSubmit(messages: any, callbacks: any) {
  const xAgent = new XAgent({
    baseURL: 'https://api.moonshot.cn/v1/chat/completions', //月之暗面
    dangerouslyApiKey: 'Bearer sk-xxxxxxxxxxxxxxx', //替换apiKey
    model: 'kimi-k2-0711-preview',
  });
  xAgent.request(
    {
      stream: true,
      messages: messages,
    },
    callbacks
  );
}
const button = document.querySelector('#submit');
button?.addEventListener('click', () => {
  const message = document.querySelector('#input') as HTMLInputElement;
  const output = document.querySelector('#output');
  let text = message?.value;
  let outputText = '';
  doSubmit([{ content: text, role: 'user' }], {
    onUpdate: (chunk: any) => {
      try {
        let message = JSON.parse(chunk.data).choices[0].delta.content;
        if (typeof message === 'string') {
          outputText += message;
          if (output) {
            output.innerHTML = marked(outputText) as any;
          }
        }
      } catch (error) {
        console.error(error);
      }
    },
    onSuccess: (chunks: any[]) => {
      console.log('chunks', chunks);
    },
    onError: (chunk: any) => {
      console.log('chunks', chunk);
    },
  });
});
